package km.TestFileExplorer;

import java.io.*;
import java.util.*;
import android.R.*;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.view.ViewGroup.*;
import android.widget.*;
import android.widget.AdapterView.*;

public class TestFileExplorer extends Activity implements OnItemClickListener{
	
	String filename;
	public final static String ROOT="/";
	public final static String TO_UP="../";
	private TextView myPath;
	private ExplorerListView vList;
	private LinearLayout vListMain;
	private File[] files;
	private Button btnHome;
	private Button btnUp;
	private Button btnAddFile;
	private File file;	
	private long fileMaxSize =1024 * 1024 * 5; //5Mbyte 
	private long fileSize =0; 

    public static final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
    public static final int MP = ViewGroup.LayoutParams.MATCH_PARENT;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.explorer_main);
        myPath = (TextView)findViewById(R.id.path);
        vListMain = (LinearLayout)findViewById(R.id.main);
        btnHome =(Button)findViewById(R.id.btn_folder_home);
        btnUp =(Button)findViewById(R.id.btn_folder_up);
        btnAddFile =(Button)findViewById(R.id.btn_add_file);
        btnHome.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				   getDir(ROOT);
			}
		});
        btnUp.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        		   getDir(file.getParent());        		
        	}
        });
        btnAddFile.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        		addFile();    		
        	}
        });
        
        if(vList==null)vList= new ExplorerListView(this);
        vListMain.addView(vList, new LayoutParams(MP,MP));
        vList.setOnItemClickListener(this);
        getDir(ROOT);
    }
    
    private void getDir(String dirPath)
    {
    	myPath.setText("Location: " + dirPath);
    	vList.checkFiles.clear();	
    	file = new File(dirPath);
    	files = file.listFiles();
       	if(dirPath.equals(ROOT)){
       		btnUp.setEnabled(false);
       		btnUp.setTextColor(Color.GRAY);
       	}else{
       		btnUp.setEnabled(true);
       		btnUp.setTextColor(Color.WHITE);
       	}    	
    	vList.initData(files);
    }
    
public void onItemClick(AdapterView<?> parent, View view, int position,
		long id) {
		File file = files[position];
		Log.e("path",file.getPath());
		if (file.isDirectory())
		{
			if(file.canRead())
				getDir(file.getPath());
			else
			{
				new AlertDialog.Builder(this)
				.setIcon(R.drawable.icon)
				.setTitle("[" + file.getName() + "] folder can't be read!")
				.setPositiveButton("OK", 
						new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
							}
						}).show();
			}
		}
		else
		{
			fileSize = file.length();
			if(fileSize>fileMaxSize){
				showAlert("5M이상은 첨부할수없습니다.");
			}else{			
				File checkFile = vList.checkFiles.get(file.toString());
				if(checkFile==null){
					vList.checkFiles.put(file.toString(),file);
	//				view.setBackgroundColor(Color.GREEN);
					TextView vFileName = (TextView)view.findViewById(R.id.item_file_name);
					TextView vFileSize = (TextView)view.findViewById(R.id.item_file_size);
					vFileName.setTextColor(Color.BLUE);
					vFileSize.setTextColor(Color.BLUE);
				}else{
					vList.checkFiles.remove(file.toString());
					TextView vFileName = (TextView)view.findViewById(R.id.item_file_name);
					TextView vFileSize = (TextView)view.findViewById(R.id.item_file_size);
					vFileName.setTextColor(Color.WHITE);
					vFileSize.setTextColor(Color.WHITE);
	//				view.setBackgroundColor(Color.BLACK);
				}
			}
		}
		Log.e("onClick","checkFiles>>"+vList.checkFiles);
	}
	private void addFile(){
		if(vList==null)	return;
		if(vList.checkFiles==null ||vList.checkFiles.size()<1)
			showAlert("체크된 파일이 없습니다.");
		else{
			ArrayList<String> fnames=new ArrayList<String>();
			ArrayList<String> fpaths=new ArrayList<String>();
			Log.e("checkFiles",vList.checkFiles+"");
			
			for(int i =0;i<files.length;i++){
				if(vList.checkFiles.get(files[i].toString())!=null){
					fnames.add(files[i].getName());
					fpaths.add(files[i].getPath());
				}
			}
			Log.e("fnames",fnames.toString());
			Intent intent = new Intent();
			intent.putStringArrayListExtra("fname", fnames);
			intent.putStringArrayListExtra("fpath", fpaths);
			 setResult(RESULT_OK, intent);
			 
			 finish();
		}		
	}
	@Override
	protected void onDestroy() {
		cleanUp();		
		super.onDestroy();
	}
	private void cleanUp(){
		if(vList!=null){
			vList.cleanUp();
		}
			vList = null;
	}
	private void showAlert(String msg){
	    try{
	        new AlertDialog.Builder(this)
	        .setTitle("알림")
	        .setMessage(msg)
	        .setCancelable(false)
	        .setPositiveButton(string.ok,
	                new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                        dialog.dismiss();
	                    }
	                }).show();
	    }catch (Exception e) {
	    	e.printStackTrace();
		}
	}
}